﻿using ControlHelper.Tools;
using ControlHelper.WPF;
using Creator;
using Creator.Core;
using Creator.Core.DataBind;
using Creator.Core.Entity;  
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes; 

namespace DesktopCreator
{
    /// <summary>
    /// ItemEditor.xaml 的交互逻辑
    /// </summary>
    public partial class ItemEditor : Window
    {
        List<BindConfig> bindConfigs = new List<BindConfig>(); 
        public ItemEditor()
        {
            InitializeComponent();
          
           
            bindConfigs = JsonHelper.ReadJson<List<BindConfig>>(Directory.GetCurrentDirectory() + @"\DataBindConfig.json", true);

            if (bindConfigs == null) bindConfigs = new List<BindConfig>();
        
            var names = Utility.Reflection.GetClassDataTypes(typeof(ViewModelBase));
            var types = bindConfigs.Select(a => a.TargetType).ToList();
            var newTypes = names.Except(types);

            cmbTypes.ItemsSource = names.ToList();

            foreach (var item in newTypes)
            {
                bindConfigs.Add(new BindConfig()
                {
                    TargetType = item,
                });
            }

           
            cmbTypes.SelectionChanged += (s, e) =>
            {
                if (!(s is ComboBox b)) return;
                var item = b.SelectedItem as Type;
                var type = bindConfigs.Where(a => a.TargetType == item).FirstOrDefault();
                


                var props = item.GetProperties();
                spProperty.Children.Clear(); 
                spPropList.Children.Clear();

                foreach (var prop in type.Binds)
                {
                    //选择需要绑定的
                    var tb = new TextBlock()
                    {
                        Text = prop.PropertyName,
                        Margin = new Thickness(5)
                    };

                    tb.MouseDown += (ps, pe) =>
                    {
                        type.RemoveBind(tb.Text);
                        spPropList.Children.Remove(tb);
                    };
                    spPropList.Children.Add(tb);
                }


                foreach (var prop in props)
                {
                    TextBlock textBlock = new TextBlock();
                    textBlock.Margin = new Thickness(5);
                    textBlock.Text = prop.Name;
                    textBlock.MouseDown += (ms, me) =>
                    { 
                        var chiTB = spPropList.Children.OfType<TextBlock>().Where(a => a.Text == prop.Name).FirstOrDefault();
                        if (chiTB != null) return;

                        //选择需要绑定的
                        var tb = new TextBlock()
                        {
                            Text = prop.Name,
                            Margin = new Thickness(5)
                        };
                        tb.MouseDown += (ps, pe) =>
                        {
                            type.RemoveBind(tb.Text);
                            spPropList.Children.Remove(tb);
                        };
                        type.AddBind(prop.Name);
                        spPropList.Children.Add(tb);
                    };
                    spProperty.Children.Add(textBlock);
                }

            };
        }
  
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            JsonHelper.WriteJson(bindConfigs, Directory.GetCurrentDirectory() + @"\DataBindConfig.json");
        } 
       
    }
}
